package com.example.twoscroolview.utils

import android.graphics.Canvas
import android.renderscript.AllocationAdapter
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView
import com.example.twoscroolview.adapter.SlideAdapter
import com.example.twoscroolview.adapter.StringAdapter

/**
 * 卡片的滑动
 * 15 上下左右都可以
 */
class SlideCardCallBack(
    val recyclerView: RecyclerView,
    val adapter: SlideAdapter

):ItemTouchHelper.SimpleCallback(0,15) {
    //拖拽
    override fun onMove(
        recyclerView: RecyclerView,
        viewHolder: RecyclerView.ViewHolder,
        target: RecyclerView.ViewHolder
    ): Boolean {
     return false
    }

    override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
        //划走的数据放到最下面
        val remove = adapter.data.removeAt(viewHolder.layoutPosition)
       // adapter.remove(remove)
        adapter.addData(7,remove)
        adapter.notifyDataSetChanged()
    }
    fun startAnim(){

    }
    override fun onChildDraw(
        c: Canvas,
        recyclerView: RecyclerView,
        viewHolder: RecyclerView.ViewHolder,
        dX: Float,
        dY: Float,
        actionState: Int,
        isCurrentlyActive: Boolean
    ) {
        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
        val maxDistance = recyclerView.width*0.5f
        val distance = Math.sqrt((dX*dX+dY*dY).toDouble())
        var fraction = distance/maxDistance
        if(fraction>1){
            fraction= 1.0
        }
        val itemCount = recyclerView.childCount
        for (i in 0 until  itemCount)
        {
            val view = recyclerView.getChildAt(i)
            val level = itemCount - i - 1
            if (level > 0) {
                if (level < CardConfig.MAX_SHOW_COUNT - 1) {
                    view.setTranslationY((CardConfig.TRANS_Y_GAP *level  - fraction*CardConfig.SCALE_GAP).toFloat())
                    view.setScaleX((1 - CardConfig.SCALE_GAP * level + fraction*CardConfig.SCALE_GAP).toFloat())
                    view.setScaleY((1 - CardConfig.SCALE_GAP * level + fraction*CardConfig.SCALE_GAP).toFloat())
                }
            }
        }

    }

    override fun getAnimationDuration(
        recyclerView: RecyclerView,
        animationType: Int,
        animateDx: Float,
        animateDy: Float
    ): Long {
        return 500
    }
}